package com.mofang.powerdekorhelper.activity.card;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.model.CardDetail;
import com.mofang.powerdekorhelper.persenter.CardDetailPersenter;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.view.CardDetailView;
import com.mofang.powerdekorhelper.witget.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by luofei on 2017/8/23 0023.
 */

public class CardDetailActivity extends MvpActivity<CardDetailView, CardDetailPersenter> implements CardDetailView {

    @BindView(R.id.card_detail_titleBar)
    TitleBar mTitleBar;

    @BindView(R.id.card_detail_header)
    RelativeLayout mHeader;

    @BindView(R.id.card_detail_value)
    TextView mValue;

    @BindView(R.id.card_detail_value_flag)
    TextView mValueFlag;

    @BindView(R.id.card_detail_price)
    TextView mPrice;

    @BindView(R.id.card_detail_type)
    TextView mType;

    @BindView(R.id.card_detail_salenum)
    TextView mNum;

    @BindView(R.id.card_detail_left)
    TextView mLeft;

    @BindView(R.id.card_detail_explain)
    TextView mExplain;


    @BindView(R.id.card_use_group)
    RelativeLayout mUseGroup;

    @BindView(R.id.card_use_tv)
    TextView mUseTv;

    @BindView(R.id.card_detail_red_title)
    TextView mInfoTitle;

    @BindView(R.id.card_detail_buy)
    Button mBuy;

    private int type;
    private int card_id;
    private Dialog mDialog;

    @Override
    public void initView() {
        inflateLayout(R.layout.activity_card_detail_layout);
        initTitleBar();
    }

    @Override
    public void initData() {
        type = getIntent().getIntExtra("type", 0);
        card_id = getIntent().getIntExtra("card_id", 0);

        switch (type) {
            case 1:
                mValueFlag.setVisibility(View.VISIBLE);

                mType.setText(R.string.card_redbag_text);
                mInfoTitle.setText(R.string.redbag_info_text);
                mHeader.setBackgroundDrawable(getResources().getDrawable(R.mipmap.redbag_info_bg));
                break;

            case 2:
                mValueFlag.setVisibility(View.VISIBLE);

                mType.setText(R.string.coupon_text);
                mInfoTitle.setText(R.string.coupon_info_text);
                mHeader.setBackgroundDrawable(getResources().getDrawable(R.mipmap.coupon_info_bg));
                break;

            case 3:
                mValueFlag.setVisibility(View.GONE);

                mType.setText(R.string.discount_text);
                mInfoTitle.setText(R.string.discount_info_text);
                mHeader.setBackgroundDrawable(getResources().getDrawable(R.mipmap.discount_info_bg));
                break;

            default:
                break;
        }
        if (card_id != 0) {
            presenter.getCardDetail(getRequestParam(true).toString());
        }
    }

    /**
     * 请求参数
     *
     * @return
     */
    private JSONObject getRequestParam(boolean tag) {
        JSONObject jsonObject = new JSONObject();
        try {
            if (SharePerforenceUtils.getInstance(this).getLogin()) {
                jsonObject.put("customer_id", SharePerforenceUtils.getInstance(this).getUserID());
            }
            if (tag) {
                jsonObject.put("id", card_id);
            } else {
                jsonObject.put("coupons_id", card_id);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    @OnClick({R.id.card_use_group})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.card_use_group:
                startActivity(new Intent(this, CardUseActivity.class).putExtra("card_id", card_id));
                break;

            default:
                break;
        }
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void setCardDetail(CardDetail detail) {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void toast(CharSequence s) {

    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {

    }

    @Override
    public CardDetailPersenter initPresenter() {
        return new CardDetailPersenter();
    }

    private void initTitleBar() {
        initTitleBar(mTitleBar, -1);
        mTitleBar.setLeftImageResource(R.mipmap.activity_title_back_koala);
        mTitleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
